<template>
  <div class="commentList">
    <van-cell title="全部评论"></van-cell>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <commentItem
        v-for="(comment, index) in list"
        :key="index"
        :comment="comment"
      />
    </van-list>
  </div>
</template>

<script>
import { getComments } from "../api/comment.js";
import commentItem from "./commentItem";
export default {
  name: "commentList",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    };
  },
  props: {
    source: {
      type: [Number, String, Object],
      required: true
    }
  },
  components: {
    commentItem
  },
  methods: {
    async onLoad() {
      //   请求获取数据
      // 把数据放到列表中
      // 本次的loading关闭
      // 判断是否还有数据
      // 有执行下一次数据的页码
      // 没有finished设置为true
      const { res } = getComments({
        type: "a",
        source: this.source,
        offset: this.offset,
        limit: this.limit
      });
      // console.log(res)
      const { results } = res.data;
      this.list.push(...results);

      this.loading = false;

      if (results.length) {
        this.offset = res.data.last_id;
      } else {
        this.finished = true;
      }
    }
  }
};
</script>
<style scoped></style>
